package com.kangtushizhe.controller;

import com.kangtushizhe.emuns.Cart;
import com.kangtushizhe.entities.CommonResult;
import com.kangtushizhe.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-23
 */
@RestController
@RequestMapping("/cart")
@Tag(name = "小车")
public class CartController {
    @Resource
    private CartService cartService;

    //获取所有小车信息
    @GetMapping("/all")
    @Operation(summary = "获取所有小车", description = "() => List<Cart> carts")
    public CommonResult getAllCarts() {
        List<Cart> allCarts = cartService.list();
        return new CommonResult(200, "获取所有小车成功", allCarts);
    }

    //创建新小车
    @PostMapping("/")
    @Operation(summary = "创建新小车", description = "() => 创建新小车")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult createCart() {
        cartService.save(new Cart(null, 100, null, 1));
        return new CommonResult(200, "创建小车成功");
    }

    //根据id获取小车信息
    @GetMapping("/id")
    @Operation(summary = "根据id获取小车信息", description = "(Integer id) => Cart cart")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult updateCartById(@RequestParam(required = true) Integer id) {
        Cart cart = cartService.getById(id);
        if (cart != null) {
            return new CommonResult(200, "根据id:" + id + ",获取小车成功", cart);
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到小车");
        }
    }

    //根据id更新小车信息
    @PutMapping("/id")
    @Operation(summary = "根据id更新小车信息", description = "(Integer id,?Integer electricity,?String position,?Integer state) => 更新小车信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "electricity", description = "电量", required = false),
            @Parameter(name = "position", description = "位置", required = false),
            @Parameter(name = "state", description = "状态", required = false),
    })
    public CommonResult getCartById(@RequestParam Integer id,
                                    @RequestParam(required = false) Integer electricity,
                                    @RequestParam(required = false) String position,
                                    @RequestParam(required = false) Integer state) {
        Cart cart = cartService.getById(id);
        if (cart != null) {
            cartService.updateById(new Cart(id, electricity, position, state));
            return new CommonResult(200, "根据id:" + id + ",更新小车信息成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到小车");
        }
    }

    //根据id移除小车信息
    @DeleteMapping("/id")
    @Operation(summary = "根据id移除小车信息", description = "(Integer id) => 根据id移除小车信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult removeCartById(@RequestParam(required = true) Integer id) {
        Cart cart = cartService.getById(id);
        if (cart != null) {
            cartService.removeById(id);
            return new CommonResult(200, "根据id:" + id + ",移除小车成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到小车");
        }
    }


}
